/**
 * Copyright 2010 Erlacher Felix, Estgfaeller Wolfgang, Ferula Patrick
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package at.socialconference.app;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TableRow;
import android.widget.TextView;
import at.socialconference.app.connection.ConnectionHandler;
import at.socialconference.app.model.AUser;
import at.socialconference.inter.connection.protocol.SocketProtocol;

public class PeopleActivity extends Activity implements OnClickListener {

	private static final int ID_DIALOG_FRREQUEST = 0;
	private TableRow rowCountry;
	private TableRow rowCity;
	private TableRow rowAddress;
	private TextView country;
	private TextView city;
	private TextView address,name;
	private Button dialBtn, webpageBtn, dblpBtn, mailBtn, friendReqBtn;
	private AUser user;
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.peopleview); 
		rowCountry = (TableRow) findViewById(R.id.pv_trcountry);
		rowCity = (TableRow) findViewById(R.id.pv_trcity);
		rowAddress = (TableRow) findViewById(R.id.pv_traddress);
		country = (TextView) findViewById(R.id.pv_country);
		city = (TextView) findViewById(R.id.pv_city);
		address = (TextView) findViewById(R.id.pv_address);
		name = (TextView) findViewById(R.id.pv_trname);
		dialBtn = (Button) findViewById(R.id.pv_call);
		webpageBtn = (Button) findViewById(R.id.pv_webpage);
		dblpBtn = (Button) findViewById(R.id.pv_dblp);
		mailBtn = (Button) findViewById(R.id.pv_mail);
		friendReqBtn = (Button) findViewById(R.id.pv_frequest);
		friendReqBtn.setVisibility(View.GONE);
		Bundle b = getIntent().getExtras(); //Get the intent's extras
		user = (AUser)b.getParcelable("person");

		dialBtn.setOnClickListener(this);
		webpageBtn.setOnClickListener(this);
		dblpBtn.setOnClickListener(this);
		mailBtn.setOnClickListener(this);
		friendReqBtn.setOnClickListener(this);


		setContent();
	}
	private void setContent() {
		String n = user.getTitle()==null?"":user.getTitle()+" ";
		n += user.getFirstname()==null?"":user.getFirstname()+" ";
		n += user.getLastname()==null?"":user.getLastname()+" ";
		name.setText(n);

		n = user.getCountry();
		if(n==null||n.length()==0){
			rowCountry.setVisibility(View.GONE);
		}else{
			country.setText(n);
		}

		n = user.getCity();
		if(n==null||n.length()==0){
			rowCity.setVisibility(View.GONE);
		}else{
			city.setText(n);
		}

		n = user.getAddress();
		if(n==null||n.length()==0){
			rowAddress.setVisibility(View.GONE);

		}else{
			address.setText(n);
		}


		n = user.getPhone();
		if(n==null||n.length()==0){
			dialBtn.setVisibility(View.GONE);
		}

		n = user.getWebpage();
		if(n==null||n.length()==0){
			webpageBtn.setVisibility(View.GONE);
		}

		n = user.getDblp();
		if(n==null||n.length()==0){
			dblpBtn.setVisibility(View.GONE);
		}

		n = user.getMail();
		if(n==null||n.length()==0){
			mailBtn.setVisibility(View.GONE);
		}

		//		Thread t = new Thread(){
		//			public void run(){
		int friendStatus = ConnectionHandler.checkFriendStatus(user.getId());		
		if(friendStatus==SocketProtocol.RESP_FSTATUS_NOFRIENDSHIP){
			friendReqBtn.setVisibility(View.VISIBLE);
		}
	}
	//		};
	//		t.start(); 
	//}
	@Override
	public void onClick(View v) {
		if(v.equals(dialBtn)){
			Intent dialIntent = new Intent(Intent.ACTION_DIAL, Uri
					.parse("tel:"+user.getPhone()));
			startActivity(dialIntent);
		}else if(v.equals(webpageBtn)){
			Intent i = new Intent(Intent.ACTION_VIEW);
			i.setData(Uri.parse(user.getWebpage()));
			startActivity(i);
		}else if(v.equals(dblpBtn)){
			Intent i = new Intent(Intent.ACTION_VIEW);
			i.setData(Uri.parse(user.getDblp()));
			startActivity(i);
		}else if(v.equals(mailBtn)){
			Intent i = new Intent(android.content.Intent.ACTION_SEND);
			i.setType("plain/text");
			i.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{user.getMail()});
			startActivity(i);
		}else if(v.equals(friendReqBtn)){
			friendRequest();
			friendReqBtn.setVisibility(View.GONE);
		}
	}

	private void friendRequest() {
		showDialog(ID_DIALOG_FRREQUEST);
		Thread t = new Thread() {
			public void run() {
				ConnectionHandler.friendRequest(user.getId());
				dismissDialog(ID_DIALOG_FRREQUEST);
			}
		};

		t.start();
	}
	
	protected Dialog onCreateDialog(int id) {
		if (id == ID_DIALOG_FRREQUEST) {
			ProgressDialog loadingDialog = new ProgressDialog(this);
			loadingDialog.setTitle("Please wait");
			loadingDialog.setMessage("Processing FriendRequest");
			loadingDialog.setIndeterminate(true);
			loadingDialog.setCancelable(true);
			return loadingDialog;
		}
		return super.onCreateDialog(id);
	}
}
